home *** CD-ROM | disk | FTP | other *** search
Text File | 1991-05-01 | 7.2 KB | 319 lines | [TEXT/MPS ] |
- #include "PascalString.h"
-
- #ifndef __STDIO__
- #include <StdIo.h>
- #endif
-
- #ifndef __STRING__
- #include <String.h>
- #endif
-
- #pragma segment Main
-
-
- // Method definitions for the class String.
-
- // Function definitions for the string insert helper functions.
-
- void String::InsertHelper(const String& insStr,
- short pos,
- short maxLength)
- {
- short newLength = insStr.Length() + Length();
-
- // Check to see if this insertion will result in a string that is longer
- // than the maximum length of this string. If so then truncate characters
- // from the right side of this string before doing the insertion.
-
- if (newLength > maxLength)
- {
- short truncLength = newLength - maxLength;
- Delete(maxLength - truncLength + 1, truncLength);
- newLength = maxLength;
- }
-
- // Memmove can be used on overlapping strings, which is what we have here,
- // as opposed to memcpy which cannot.
-
- memmove(&fStr[pos + insStr.Length()], &fStr[pos], Length() - pos + 1);
- memmove(&fStr[pos], &insStr.fStr[1], insStr.Length());
- Length() = (unsigned char)newLength;
- }
-
-
- void String::InsertHelper(const char* insStr,
- short pos,
- short maxLength)
- {
- #if qDebugMsg
- if (pos > Length())
- fprintf(stderr, "###String::InsertHelper: Insert position greater than length of string.\n");
- #endif
-
- short insLength = insStr == NULL ? 0 : strlen(insStr);
- short newLength = insLength + Length();
-
- // Check to see if this insertion will result in a string that is longer
- // than the maximum length of this string. If so then truncate characters
- // from the right side of this string before doing the insertion.
-
- if (newLength > maxLength)
- {
- #if qDebugMsg
- fprintf(stderr, "### String::InsertHelper: String truncated during insert call.\n");
- #endif
- short truncLength = newLength - maxLength;
- Delete(maxLength - truncLength + 1, truncLength);
- newLength = maxLength;
- }
-
- // Memmove can be used on overlapping strings, which is what we have here,
- // as opposed to memcpy which cannot.
-
- memmove(&fStr[pos + insLength], &fStr[pos], Length() - pos + 1);
- memmove(&fStr[pos], insStr, insLength);
- Length() = (unsigned char)newLength;
- }
-
- unsigned char& String::operator[](short pos)
- // !!! we'd ideally like this method to be inlined but CFront doesn't seem to
- // want to inline it for us !!!
- {
- return fStr[pos];
- } // for non-const String
-
- String::operator char*() const
- {
- // Note: This method cannot be used in a multi-thread environment, since the following static
- // storage is used everytime this method is called. For now this seams robust enough, since
- // it is mainly used to present debugging information.
-
- static char cString[kStr255Len+1];
-
- strncpy(cString, (char *) &fStr[1], Length());
- cString[Length()] = '\0';
-
- return cString;
- }
-
- // Method definitions for the class Str255
-
- Str255::Str255(const char* str)
- {
- // Truncate the C String to 255 bytes if necessary.
-
- Length() = str == NULL ? 0 : strlen((const char*)str);
-
- if (Length() > kStr255Len)
- Length() = kStr255Len;
- memcpy(&fStr[1], str, Length());
- }
-
-
- Str255 Str255::Copy(short pos, short length)
- {
- Str255 newString;
-
- length = length > Length() - pos + kLengthByte ? Length() - pos + kLengthByte : length;
- memcpy(&newString.fStr[1], &fStr[pos], length);
- newString.Length() = length;
- return newString;
- }
-
- Str255 operator+(const String& s1,
- const char* s2)
- {
- Str255 newStr;
-
- memcpy(newStr.fStr, s1.fStr, s1.Length() + kLengthByte);
- newStr.Length() += s2 == NULL ? 0 : strlen((const char*)s2);
- if (newStr.Length() > kStr255Len)
- newStr.Length() = kStr255Len;
- memcpy(&newStr.fStr[s1.Length() + kLengthByte], s2, newStr.Length() - s1.Length());
-
- return newStr;
- }
-
-
- Str255 operator+(const char* s1,
- const String& s2)
- {
- Str255 newStr;
- short cLen = s2 == NULL ? 0 : strlen((const char*)s1);
-
- memcpy(&newStr[1], s1, cLen);
- newStr.Length() = s2.Length() + cLen;
- if (newStr.Length() > kStr255Len)
- newStr.Length() = kStr255Len;
- memcpy(&newStr[cLen + kLengthByte], s2.fStr + 1, newStr.Length() - cLen);
-
- return newStr;
- }
-
-
- Str255 operator+(const String& s1,
- const String& s2)
- {
- Str255 newStr;
-
- memcpy(newStr.fStr, s1.fStr, s1.Length() + kLengthByte);
- newStr.Length() += s2.Length();
- if (newStr.Length() > kStr255Len)
- newStr.Length() = kStr255Len;
- memcpy(&newStr.fStr[s1.Length() + kLengthByte], s2.fStr + 1, newStr.Length() - s1.Length());
-
- return newStr;
- }
-
-
- // Method definitions for the concatenation operator += (Str255).
-
- Str255& Str255::operator += (const String& str)
- {
- InsertHelper (str, Length() + 1, kStr255Len);
- return *this;
- }
-
- Str255& Str255::operator += (const char* str)
- {
- InsertHelper (str, Length() + 1, kStr255Len);
- return *this;
- }
-
- Str255& Str255::operator += (const char ch)
- {
- if (++Length() <= kStr255Len)
- fStr[Length()] = ch;
- else
- {
- --Length();
- #if qDebugMsg
- fprintf(stderr, "###Str255::operator+=: Concatenation produces str255 overflow.\n");
- #endif
- }
-
- return *this;
- }
-
-
- // Method definitions for Str63.
-
- Str63::Str63(const char* str)
- {
- // Truncate the C String to 63 bytes if necessary.
-
- Length() = str == NULL ? 0 : strlen((const char*)str);
- if (Length() > kStr63Len)
- Length() = kStr63Len;
- memcpy(&fStr[1], str, Length());
- }
-
- Str63 Str63::Copy(short pos, short length)
- {
- Str63 newString;
-
- length = length > Length() - pos + kLengthByte ? Length() - pos + kLengthByte : length;
- memcpy(&newString.fStr[1], &fStr[pos], length);
- newString.Length() = length;
- return newString;
- }
-
- Str63& Str63::operator += (const String& str)
- {
- InsertHelper (str, Length() + 1, kStr63Len);
- return *this;
- }
-
- Str63& Str63::operator += (const char* str)
- {
- InsertHelper (str, Length() + 1, kStr63Len);
- return *this;
- }
-
- Str63& Str63::operator += (const char ch)
- {
- if (++Length() <= kStr63Len)
- fStr[Length()] = ch;
- else
- {
- --Length();
- #if qDebugMsg
- fprintf(stderr, "###Str63::operator+=: Concatenation produces str63 overflow.\n");
- #endif
- }
-
- return *this;
- }
-
-
- // Method definitions for Str32.
-
- Str32::Str32(const char* str)
- {
- // Truncate the C String to 32 bytes if necessary.
-
- Length() = str == NULL ? 0 : strlen((const char*)str);
- if (Length() > kStr32Len)
- Length() = kStr32Len;
- memcpy(&fStr[1], str, Length());
- }
-
- Str32 Str32::Copy(short pos, short length)
- {
- Str32 newString;
-
- length = length > Length() - pos + kLengthByte ? Length() - pos + kLengthByte : length;
- memcpy(&newString.fStr[1], &fStr[pos], length);
- newString.Length() = length;
- return newString;
- }
-
- // Method definitions for Str31.
-
- Str31::Str31(const char* str)
- {
- // Truncate the C String to 31 bytes if necessary.
-
- Length() = str == NULL ? 0 : strlen((const char*)str);
- if (Length() > kStr31Len)
- Length() = kStr31Len;
- memcpy(&fStr[1], str, Length());
- }
-
- Str31 Str31::Copy(short pos, short length)
- {
- Str31 newString;
-
- length = length > Length() - pos + kLengthByte ? Length() - pos + kLengthByte : length;
- memcpy(&newString.fStr[1], &fStr[pos], length);
- newString.Length() = length;
- return newString;
- }
-
- Str31& Str31::operator += (const String& str)
- {
- InsertHelper (str, Length() + 1, kStr31Len);
- return *this;
- }
-
- Str31& Str31::operator += (const char* str)
- {
- InsertHelper (str, Length() + 1, kStr31Len);
- return *this;
- }
-
- Str31& Str31::operator += (const char ch)
- {
- if (++Length() <= kStr31Len)
- fStr[Length()] = ch;
- else
- {
- --Length();
- #if qDebugMsg
- fprintf(stderr,"###Str31::operator+=: Concatenation produces str31 overflow.\n");
- #endif
- }
-
- return *this;
- }
-